package com.mengjiahui.auduoplayer.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.mengjiahui.auduoplayer.R;
import com.mengjiahui.auduoplayer.bean.Music;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Meng
 * @data 2020/5/19
 */
public class MusicRankInfoAdapter extends RecyclerView.Adapter<MusicRankInfoAdapter.ViewHolder>{
    private OnItemClickListener onItemClickListener;
    private Context context;
    private List<Music> musicList;
    private View view;

    public MusicRankInfoAdapter(List<Music> musicList){
        this.musicList=musicList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context=parent.getContext();
        view= LayoutInflater.from(context).inflate(R.layout.item_music_rank_info,parent,false);
        MusicRankInfoAdapter.ViewHolder viewHolder=new MusicRankInfoAdapter.ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Music music=musicList.get(position);
        holder.textViewMusicRankInfoAuthorAlbum.setText(music.getAuthor());
        holder.textViewMusicRankInfoName.setText(music.getMusicName());
        Glide.with(context).load(music.getPath()).into(holder.imageViewMusicRankInfoIcon);
        holder.itemView.setOnClickListener(v->{
                onItemClickListener.touch(music);
        });
    }

    @Override
    public int getItemCount() {
        return musicList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView imageViewMusicRankInfoIcon;
        private TextView textViewMusicRankInfoName;
        private TextView textViewMusicRankInfoAuthorAlbum;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageViewMusicRankInfoIcon=itemView.findViewById(R.id.imageViewMusicRankInfoIcon);
            textViewMusicRankInfoName=itemView.findViewById(R.id.textViewMusicRankInfoName);
            textViewMusicRankInfoAuthorAlbum=itemView.findViewById(R.id.textViewMusicRankInfoAuthorAlbum);
        }
    }

    public interface OnItemClickListener{
        void touch(Music music);
    }
    public void setListener(OnItemClickListener onItemClickListener) {

        this.onItemClickListener = onItemClickListener;
    }
}
